package dao;

import db.DatabaseManager;
import model.Computer;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ComputerDao {

    public List<Computer> getAllComputers() {
        List<Computer> computers = new ArrayList<>();
        String sql = "SELECT * FROM computers";

        try (Connection conn = DatabaseManager.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Computer computer = new Computer(
                        rs.getString("id"),
                        rs.getString("brand"),
                        rs.getString("model"),
                        rs.getString("configuration"),
                        rs.getDouble("price"),
                        rs.getInt("stock")
                );
                computers.add(computer);
            }
        } catch (SQLException e) {
            System.out.println("查询电脑信息失败: " + e.getMessage());
        }
        return computers;
    }

    public void addComputer(Computer computer) {
        String sql = "INSERT INTO computers(id, brand, model, configuration, price, stock) VALUES(?,?,?,?,?,?)";

        try (Connection conn = DatabaseManager.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, computer.getId());
            pstmt.setString(2, computer.getBrand());
            pstmt.setString(3, computer.getModel());
            pstmt.setString(4, computer.getConfiguration());
            pstmt.setDouble(5, computer.getPrice());
            pstmt.setInt(6, computer.getStock());
            pstmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("添加电脑失败: " + e.getMessage());
        }
    }

    public void updateComputer(Computer computer) {
        String sql = "UPDATE computers SET brand = ?, model = ?, configuration = ?, price = ?, stock = ? WHERE id = ?";

        try (Connection conn = DatabaseManager.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, computer.getBrand());
            pstmt.setString(2, computer.getModel());
            pstmt.setString(3, computer.getConfiguration());
            pstmt.setDouble(4, computer.getPrice());
            pstmt.setInt(5, computer.getStock());
            pstmt.setString(6, computer.getId());
            pstmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("更新电脑失败: " + e.getMessage());
        }
    }

    public Computer findComputerById(String id) {
        String sql = "SELECT * FROM computers WHERE id = ?";
        Computer computer = null;

        try (Connection conn = DatabaseManager.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                computer = new Computer(
                        rs.getString("id"),
                        rs.getString("brand"),
                        rs.getString("model"),
                        rs.getString("configuration"),
                        rs.getDouble("price"),
                        rs.getInt("stock")
                );
            }
        } catch (SQLException e) {
            System.out.println("查询电脑失败: " + e.getMessage());
        }
        return computer;
    }

    public void deleteComputer(String id) {
        String sql = "DELETE FROM computers WHERE id = ?";

        try (Connection conn = DatabaseManager.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, id);
            pstmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("删除电脑失败: " + e.getMessage());
        }
    }
}